package com.inspur.security.cbb3.kms.keymanager.adminManager.controller;

import com.inspur.security.cbb3.kms.annotation.SysLog;
import com.inspur.security.cbb3.kms.enums.BusinessType;
import com.inspur.security.cbb3.kms.enums.SystemCode;
import com.inspur.security.cbb3.kms.keymanager.adminManager.service.AdminManagerService;
import com.inspur.security.cbb3.kms.keymanager.secrets.po.SecretsBean;
import com.inspur.security.cbb3.kms.utils.PageRequestDate;
import com.inspur.security.cbb3.kms.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * AdminManagerController
 *
 * @author liukai
 * @date 2020/12/3
 **/
@RestController
@RequestMapping("/v1/cleanup")
@Slf4j
public class AdminManagerController {

    @Autowired
    private AdminManagerService adminManagerService;

    /**
     * 彻底删除所有逻辑删密钥
     * @return
     */
    @DeleteMapping
    @SysLog(title = "彻底删除所有逻辑删密钥", businessType = BusinessType.DELETE)
    public ResponseEntity cleanupAllLogicalDelKey(){
        adminManagerService.cleanupAllLogicalDelKey();
        return ResultUtil.noDataReturn(SystemCode.SUCCESS, HttpStatus.NO_CONTENT);
    }

    /**
     * 彻底删除密钥
     * @param uuid
     * @return
     */
    @DeleteMapping("/{uuid}")
    @SysLog(title = "彻底删除单个逻辑删密钥", businessType = BusinessType.DELETE)
    public ResponseEntity cleanupLogicalDelKey(@PathVariable("uuid") String uuid){
        adminManagerService.cleanupLogicalDelKey(uuid);
        return ResultUtil.noDataReturn(SystemCode.SUCCESS, HttpStatus.NO_CONTENT);
    }

    /**
     * 恢复逻辑删密钥
     * @param uuid
     * @return
     */
    @PutMapping("/{uuid}")
    @SysLog(title = "恢复逻辑删密钥", businessType = BusinessType.UPDATE)
    public ResponseEntity recoveryLogicalDelKey(@PathVariable("uuid") String uuid){
        SecretsBean secretsBean = adminManagerService.recoveryLogicalDelKey(uuid);
        return ResultUtil.dataReturn(SystemCode.SUCCESS,secretsBean,HttpStatus.OK);

    }

    /**
     * 分页查询逻辑删的密钥
     * @param pageRequestDate
     * @param searchParams
     * @return
     */
    @GetMapping
    public ResponseEntity searchSecrets(PageRequestDate pageRequestDate, SecretsBean searchParams)  {
        Page page = adminManagerService.searchLogicalDelKey(pageRequestDate, searchParams);
        Map<String,Object> returnMap=new HashMap<>(2);
        returnMap.put("total",page.getTotalElements());
        if(page.getTotalElements()<1){
            returnMap.put("secrets",null);
            return ResultUtil.objReturn(returnMap, HttpStatus.OK);
        }
        List<SecretsBean> list=page.getContent();
        returnMap.put("secrets",list);
        return ResultUtil.objReturn(returnMap, HttpStatus.OK);
    }
}
